package com.toggery.POS.tool;

import java.util.*;

public class test {
    private static Object[] computeMaxRepeatString(String str, int count) {

        Map<String, Integer> numberMap = new HashMap<>();
        String maxNumberKey = null;
        Integer maxNumberNumber = 0;

        for (int i = 0; i <= str.length() - count; i++) {

            String temp = str.substring(i, i + count);

            Integer charNumber = numberMap.get(temp);
            if (charNumber == null) {
                charNumber = 1;
            } else {
                charNumber++;
            }
            numberMap.put(temp, charNumber);

            if (maxNumberKey == null) {
                maxNumberKey = temp;
                maxNumberNumber = charNumber;
            } else {
                if (charNumber > maxNumberNumber) {
                    maxNumberKey = temp;
                    maxNumberNumber = charNumber;
                }
            }
        }
        return new Object[] { maxNumberKey, maxNumberNumber };
    }
    public static String computeMaxRepeatString(String str) {

        String maxNumberKey = null;
        Integer maxNumberNumber = 0;
        int len = str.length();

        if (len < 1) {
            throw new RuntimeException("string is empty");
        }

        if (len < 2) {
            return str.charAt(0) + "";
        }

        for (int i = 2; i <= len; i++) {

            Object[] maxValue = computeMaxRepeatString(str, i);

            String numberKey = (String) maxValue[0];
            Integer numberNumber = (Integer) maxValue[1];

            if (numberNumber >= maxNumberNumber || numberNumber >= 2) {
                maxNumberKey = numberKey;
                maxNumberNumber = numberNumber;
            }
        }
        return maxNumberKey;
    }
    public static void main(String[] args) {
        //测试
//        long i = 100000000;
//        int sum=0;
//        int temp=0;
//        String a="",a1="",a2="";
//        for(int j=0;i<999999999;i++){
//            a1 = (a+i).substring(0,4);
//            a2 = (a+i).substring(5,9);
//            temp=Integer.parseInt(a1+a2);
////            System.out.println(i+","+temp);
//            if(i%temp==0){
//                sum++;
//            }
//        }
//        System.out.println(sum);
        String str = "我都说了我要退款我要退款我要退款我说我要退款";
//        Scanner scanner = new Scanner(System.in);
//        String str = scanner.next();
        System.out.println(computeMaxRepeatString(str));
//        List<String> data = new ArrayList<String>();
//
//        for (int i = 0; i < str.length(); i++) {
//            String s = str.substring(i, i + 1);
//            if (!data.contains(s)) {
//                data.add(s);
//            }
//        }
//
//        String result = "";
//        for (String s : data) {
//            result += s;
//        }
//        System.out.println(result);
    }
}
